package com.linxz.framework.protocol.image;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.linxz.common.HttpConfig;
import com.linxz.framework.R;

import java.io.File;

import static com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions.withCrossFade;

/**
 * <p>
 * Function：
 * <p>
 * ver     date      		author
 * ──────────────────────────────────
 * V1.0   2018年03月10日0:55  lin_xiao_zhang@163.com
 * <p>
 * Copyright (c) 2018,  All Rights Reserved.
 *
 * @author linxz
 */
public class ImageHelper {

    /**设置图片加载策略*/
    private static final RequestOptions RECYCLER_OPTIONS =
            new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                    .placeholder(R.drawable.ic_placeholder)
                    .error(R.drawable.ic_img_error);
    /**
     * 不带默认图
     */
    public static void imageNet(Context mContext, String imageUrl, ImageView view) {
        if (!TextUtils.isEmpty(imageUrl) && !imageUrl.contains("http")){
            imageUrl= HttpConfig.BASE_URL+imageUrl;
        }
        Glide.with(mContext)
                .load(imageUrl+"")
                .apply(RECYCLER_OPTIONS)
                .into(view);
    }

    public static void imageLocal(Context mContext, String filePath, ImageView view) {
        Glide.with(mContext)
                .load(filePath)
                .apply(RECYCLER_OPTIONS)
                .into(view);
    }

    public static void imageLocal(Context mContext, File file, ImageView view) {
        Glide.with(mContext)
                .load(file)
                .apply(RECYCLER_OPTIONS)
                .into(view);
    }


    public static void imageLoadWithCorner(Context mContext, String url, ImageView view,float radius) {
        if (!TextUtils.isEmpty(url) && !url.contains("http")){
            url= HttpConfig.BASE_URL+url;
        }
        CornerTransform cornerTransform=new CornerTransform(mContext,radius);
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .error(R.drawable.ic_img_error)
                .transform(cornerTransform);
        Glide.with(mContext)
                .load(url)
                .apply(options)
                .into(view);
    }

    /**
     * 部分图片圆角
     * */
    public static void imageLoadWithCorner(Context mContext,
                                            String url,
                                            ImageView view,
                                            float radius,
                                            boolean leftTop,
                                            boolean rightTop,
                                            boolean leftBottom,
                                            boolean rightBottom) {
        if (!TextUtils.isEmpty(url) && !url.contains("http")){
            url= HttpConfig.BASE_URL+url;
        }
        RoundedCornersTransform transform = new RoundedCornersTransform(mContext, dip2px(radius));
        transform.setNeedCorner(leftTop, rightTop, leftBottom, rightBottom);
        RequestOptions options = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
                .error(R.drawable.ic_img_error)
                .transform(transform);
        Glide.with(mContext)
                .load(url)
                .apply(options)
                .into(view);
    }



    /**
     * 带回调的加载图片方法
     */
    public static void displayImageForView(ImageView imageView, String url) {
        Glide.with(imageView.getContext())
                .asBitmap()
                .load(url)
                .apply(initCommonRequestOption())
                .transition(withCrossFade())
                .into(imageView);
    }

    @SuppressLint("CheckResult")
    private static RequestOptions initCommonRequestOption() {
        RequestOptions options = new RequestOptions();
                options
                .placeholder(R.drawable.ic_placeholder)
                .error(R.drawable.ic_img_error);
        return options;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(float dpValue) {
        float density = Resources.getSystem().getDisplayMetrics().density;
        return (int) (0.5f + dpValue * density);
    }
}
